package com.example.schoolexam2.service.impl;

import com.example.schoolexam2.dto.LoginDto;
import com.example.schoolexam2.entity.Employee;
import com.example.schoolexam2.entity.Student;
import com.example.schoolexam2.mapper.UserMapper;
import com.example.schoolexam2.service.UserService;
import com.example.schoolexam2.utils.JwtUtils;
import com.example.schoolexam2.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper mapper;


    /**
     * 通过请求的学生用户名查询是否有该用户，
     * 如果有,通过工具类将参数密码加密，看是否与数据库密码校相同，相同登录成功，反之失败
     *
     * @param dto 封装的请求参数
     * @return
     */
    @Override
    public String userLogin(LoginDto dto) {
        try {
            Student student = mapper.studentSql(dto.getUserID());
            if (student == null) {
                Employee employee = mapper.employeeSql(dto.getUserID());
                if (employee == null) {
                    return null;
                } else if (employee != null) {
                    if (dto.getPassword().equals(employee.getPassword())) {
                        return JwtUtils.employeegenJsonWebToken(employee);
                    } else {
                        return null;
                    }
                }
            } else if (student != null) {
                if (JwtUtils.addM(dto.getPassword()).equals(student.getPassword())) {
                    return JwtUtils.studentgenJsonWebToken(student);
                } else {
                    return null;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
